﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class MenberSelect : Form
    {
        MainForm main;
        public MenberSelect(MainForm mainform)
        {
            InitializeComponent();
            main = mainform;
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        string size = "23";
        string num = "1";
        string deyeshu;
        private void MenberSelect_Load(object sender, EventArgs e)
        {
            label10.ForeColor = Color.Red;
            this.timer1.Start();
            cbxselectcondition.Items.Add("所有会员");
            cbxselectcondition.Items.Add("会员编号");
            cbxselectcondition.Items.Add("会员卡号");
            cbxselectcondition.Items.Add("身份证号");
            cbxselectcondition.Text = "所有会员";

            cbxstatus.Items.Add("正常");
            cbxstatus.Items.Add("挂失");
            cbxstatus.Items.Add("停用");
            cbxgender.Items.Add("男");
            cbxgender.Items.Add("女");
            CardType();
        }
        /// <summary>
        /// 得到卡片类型
        /// </summary>
        private void CardType()
        {
            consumeSolution consume = new consumeSolution();
            DataTable dt = consume.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cbxcardtype.Items.Contains(dt.Rows[i]["solutionName"].ToString()))
                {
                    cbxcardtype.Items.Add(dt.Rows[i]["solutionName"].ToString());

                }
            }
        }
        public void CurrYe(string strWhere)
        {
            memberInfo member = new memberInfo();
            DataTable dt = member.GetList(strWhere).Tables[0];
            int st = dt.Rows.Count;
            deyeshu = (st / decimal.Parse(size)).ToString("F2");
            int inyeshu = st / Convert.ToInt32(size);
            if (Convert.ToDouble(deyeshu) > inyeshu)
            {
                inyeshu += 1;
                deyeshu = inyeshu.ToString();
            }
            if (Convert.ToDouble(deyeshu) == inyeshu)
            {
                deyeshu = inyeshu.ToString();
            }
            MainForm mf = new MainForm();
            main.txtAlto.Text = deyeshu;
            main.txtCurr.Text = num;
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {

            MainForm.num = "1";
            SelectMenberinfo();

            this.Close();



        }
        /// <summary>
        /// 对mainform页面进行刷新查找
        /// </summary>
        public void SelectMenberinfo()
        {
            BLL.memberInfo memberInfoBll = new memberInfo();
            StringBuilder sb = new StringBuilder();
            string tiaojian1 = "";
            string Dafu1 = "";
            string tiaojian2 = "";
            string Dafu2 = "";
            string tiaojian3 = "";
            string Dafu3 = "";
            if (textBox1.Text != "")
            {
                sb.Append("memberId='" + textBox1.Text + "'");
                sb.Append("and" + " ");
            }
            if (txtname.Text != "")
            {
                sb.Append("memberName='" + txtname.Text + "'");
                sb.Append("and" + " ");
            }
            if (txttel.Text != "")
            {
                sb.Append("Tel='" + txttel.Text + "'");
                sb.Append("and" + " ");
            }
            foreach (Control item in Controls)
            {
                string cmb1 = item.Name;
                if (cmb1 == "cmbTiaojian1" && item.Visible == true)
                {
                    tiaojian1 = item.Text;
                }
                if (cmb1 == "cmbTiaojian2" && item.Visible == true)
                {
                    tiaojian2 = item.Text;
                }
                if (cmb1 == "cmbTiaojian3" && item.Visible == true)
                {
                    tiaojian3 = item.Text;
                }
                if (cmb1 == "txtDafu1" && item.Visible == true)
                {
                    Dafu1 = item.Text;
                }
                if (cmb1 == "txtDafu2" && item.Visible == true)
                {
                    Dafu2 = item.Text;
                }
                if (cmb1 == "txtDafu3" && item.Visible == true)
                {
                    Dafu3 = item.Text;
                }
            }
            if (Dafu1 == "男")
            {
                Dafu1 = "1";
            }
            if (Dafu1 == "女")
            {
                Dafu1 = "0";
            }
            if (Dafu2 == "男")
            {
                Dafu2 = "1";
            }
            if (Dafu2 == "女")
            {
                Dafu2 = "0";
            }
            if (Dafu3 == "男")
            {
                Dafu3 = "1";
            }
            if (Dafu3 == "女")
            {
                Dafu3 = "0";
            }
            if (tiaojian1 != "")
            {
                switch (tiaojian1)
                {
                    case "性    别":
                        sb.Append("sex='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "联系电话":
                        sb.Append("Tel='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "身份证号":
                        sb.Append("IDNumber='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "电子邮箱":
                        sb.Append("Email='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                }
            }
            if (tiaojian2 != "")
            {
                switch (tiaojian2)
                {
                    case "性    别":
                        sb.Append("sex='" + Dafu2 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "联系电话":
                        sb.Append("Tel='" + Dafu2 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "身份证号":
                        sb.Append("IDNumber='" + Dafu2 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "电子邮箱":
                        sb.Append("Email='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                }
            }
            if (tiaojian3 != "")
            {
                switch (tiaojian3)
                {
                    case "性    别":
                        sb.Append("sex='" + Dafu3 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "联系电话":
                        sb.Append("Tel='" + Dafu3 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "身份证号":
                        sb.Append("IDNumber='" + Dafu3 + "'");
                        sb.Append("and" + " ");
                        break;
                    case "电子邮箱":
                        sb.Append("Email='" + Dafu1 + "'");
                        sb.Append("and" + " ");
                        break;
                }
            }
            //if (cbxselectcondition.Text == "会员编号")
            //{
            //    sb.Append("memberId='" + txtkeywords.Text + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxselectcondition.Text == "会员卡号")
            //{
            //    sb.Append("memberCardId='" + txtkeywords.Text + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxselectcondition.Text == "身份证号")
            //{
            //    sb.Append("IDNumber='" + txtkeywords.Text + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxgender.Text == "男")
            //{
            //    sb.Append("sex='" + 1 + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxgender.Text == "女")
            //{
            //    sb.Append("sex='" + 0 + "'");
            //    sb.Append("and" + " ");
            //}
            //if (!string.IsNullOrEmpty(txtname.Text))
            //{
            //    sb.Append("memberName='" + txtname.Text + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxstatus.Text == "正常")
            //{
            //    sb.Append("status='" + 0 + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxstatus.Text == "挂失")
            //{
            //    sb.Append("status='" + 1 + "'");
            //    sb.Append("and" + " ");
            //}
            //if (cbxstatus.Text == "停用")
            //{
            //    sb.Append("status='" + 2 + "'");
            //    sb.Append("and" + " ");
            //}
            //if (!string.IsNullOrEmpty(txttel.Text))
            //{
            //    sb.Append("Tel='" + txttel.Text + "'");
            //    sb.Append("and" + " ");
            //}
            //if (!string.IsNullOrEmpty(cbxcardtype.Text))
            //{
            //    consumeSolution consume = new consumeSolution();
            //    Model.consumeSolution consumeMod = new Model.consumeSolution();
            //    consumeMod = consume.GetModelList("solutionName='" + cbxcardtype.Text + "'")[0];
            //    sb.Append("cardType=" + consumeMod.id);
            //    sb.Append("and" + " ");
            //}
            //if (cbxcreatetime.Checked == true)
            //{
            //    if (!string.IsNullOrEmpty(datecreatetime.Text))
            //    {
            //        sb.Append("createDate='" + DateTime.Parse(datecreatetime.Text) + "'");

            //    }
            //}


            string xxbb = sb.ToString();
            if (xxbb.Length > 4)
            {
                if (xxbb.Substring(xxbb.Length - 4, 4) == "and ")
                {
                    xxbb = xxbb.Substring(0, xxbb.Length - 4);

                }
            }
            if (xxbb.Trim() == "")
            {
                MainForm.SelectMemberTJ = "";
            }
            MainForm.SelectMemberTJ = xxbb;
            DataTable dt = memberInfoBll.Mains(size, num, xxbb, "").Tables[0];

            CurrYe(xxbb);
            main.dgvMemberInfo.DataSource = dt;
            consumeSolution consume = new consumeSolution();
            Model.consumeSolution consumeMod = new Model.consumeSolution();
            Areas area = new Areas();
            ZG_member.Model.Areas areMode = new ZG_member.Model.Areas();
            foreach (DataGridViewRow dgvr in main.dgvMemberInfo.Rows)
            {
                if (dgvr.Cells["sexName"].Value != null)
                {
                    if (dgvr.Cells["sexName"].Value + "" == "女")
                    {
                        dgvr.Cells["sexName"].Value = "女";
                    }
                    else
                    {
                        dgvr.Cells["sexName"].Value = "男";
                    }
                }
                if (dgvr.Cells["status1"].Value != null)
                {
                    if (dgvr.Cells["status1"].Value + "" == "正常")
                    {
                        dgvr.Cells["Newstatus"].Value = "正常";
                    }
                    if (dgvr.Cells["status1"].Value + "" == "挂失")
                    {
                        dgvr.Cells["Newstatus"].Value = "挂失";
                    }
                    if (dgvr.Cells["status1"].Value + "" == "停用")
                    {
                        dgvr.Cells["Newstatus"].Value = "停用";
                    }
                }
                string cardType1 = dgvr.Cells["cardType1"].Value.ToString();
                if (cardType1 != "")
                {
                    int ID = int.Parse(dgvr.Cells["cardType1"].Value.ToString());
                    consumeMod = consume.GetModelList("id='" + ID + "'")[0];
                    dgvr.Cells["NewcardType"].Value = consumeMod.solutionName;
                }

                if (dgvr.Cells["AreasID"].Value != "")
                {
                    int id = int.Parse(dgvr.Cells["AreasID"].Value.ToString());
                    areMode = area.getModel(id);
                    dgvr.Cells["AreaName"].Value = areMode.AreaName;
                }
            }
        }


        private void cbxselectcondition_TextChanged(object sender, EventArgs e)
        {
            if (cbxselectcondition.Text == "所有会员")
            {
                label2.Text = "关 键 字：";
                label2.Visible = false;
                txtkeywords.Visible = false;
            }
            if (cbxselectcondition.Text == "会员编号")
            {
                label2.Text = "会员编号：";
                label2.Visible = true;
                txtkeywords.Visible = true;
            }
            if (cbxselectcondition.Text == "会员卡号")
            {
                label2.Text = "会员卡号：";
                label2.Visible = true;
                txtkeywords.Visible = true;
            }
            if (cbxselectcondition.Text == "身份证号")
            {
                label2.Text = "身份证号：";
                label2.Visible = true;
                txtkeywords.Visible = true;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void MenberSelect_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //this.button1.Top = 10;
            //this.Width = 12;
            //this.Height = 500;
            //Button btn1 = new Button();
            //btn1.Width = 100;
            //btn1.Height = 50;
            //btn1.Text = "btn1";
            //btn1.Left = 50;
            //btn1.Top = 100;
            //this.Controls.Add(btn1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string lbTj1 = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj1" && item.Visible == true)
                {
                    lbTj1 = item.Name;
                }
            }
            if (lbTj1 == "")
            {
                Label tj = new Label();
                tj.Name = "lbTj1";
                tj.Text = "条 件 一 ：";
                tj.Top = 115;
                tj.Width = 80;
                tj.Left = 16;
                if (this.Height < 280)
                {
                    this.Height += 40;
                    this.btnSelect.Top += 40;
                    this.btnClose.Top += 40;
                }
                ComboBox cmb = new ComboBox();
                cmb.Name = "cmbTiaojian1";
                cmb.Items.Add("联系电话");
                cmb.Items.Add("性    别");
                cmb.Items.Add("身份证号");
                cmb.Items.Add("电子邮箱");
                cmb.Left = 87;
                cmb.Top = 112;
                cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                Label lb = new Label();
                lb.Name = "lbMaohao1";
                lb.Text = "：";
                lb.Top = 112;
                lb.Width = 20;
                lb.Left = 210;
                TextBox tx = new TextBox();
                tx.Name = "txtDafu1";
                tx.Top = 112;
                tx.Left = 237;
                tx.Width = 120;
                Button bt = new Button();
                bt.Click += Delete;
                bt.Top = 112;
                bt.Left = 370;
                bt.Text = "删除";
                bt.Name = "btnDelete1";
                this.Controls.Add(cmb);
                this.Controls.Add(lb);
                this.Controls.Add(tx);
                this.Controls.Add(tj);
                this.Controls.Add(bt);
                return;
            }
            string lbTj2 = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj2" && item.Visible == true)
                {
                    lbTj2 = item.Name;
                }
            }
            if (lbTj2 == "")
            {
                Label tj2 = new Label();
                tj2.Name = "lbTj2";
                tj2.Text = "条 件 二 ：";
                tj2.Top = 154;
                tj2.Width = 80;
                tj2.Left = 16;
                if (this.Height < 280)
                {
                    this.Height += 40;
                    this.btnSelect.Top += 40;
                    this.btnClose.Top += 40;
                }
                ComboBox cmb2 = new ComboBox();
                cmb2.Name = "cmbTiaojian2";
                cmb2.Items.Add("联系电话");
                cmb2.Items.Add("性    别");
                cmb2.Items.Add("身份证号");
                cmb2.Items.Add("电子邮箱");
                cmb2.Left = 87;
                cmb2.Top = 154;
                cmb2.DropDownStyle = ComboBoxStyle.DropDownList;
                Label lb2 = new Label();
                lb2.Name = "lbMaohao2";
                lb2.Text = "：";
                lb2.Top = 155;
                lb2.Width = 20;
                lb2.Left = 210;
                TextBox tx2 = new TextBox();
                tx2.Name = "txtDafu2";
                tx2.Top = 154;
                tx2.Left = 237;
                tx2.Width = 120;
                Button bt2 = new Button();
                bt2.Click += Delete2;
                bt2.Top = 154;
                bt2.Left = 370;
                bt2.Text = "删除";
                bt2.Name = "btnDelete2";
                this.Controls.Add(cmb2);
                this.Controls.Add(lb2);
                this.Controls.Add(tx2);
                this.Controls.Add(tj2);
                this.Controls.Add(bt2);
                return;
            }
            string lbTj3 = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj3" && item.Visible == true)
                {
                    lbTj3 = item.Name;
                }
            }
            if (lbTj3 == "")
            {
                Label tj3 = new Label();
                tj3.Name = "lbTj3";
                tj3.Text = "条 件 三 ：";
                tj3.Top = 196;
                tj3.Width = 80;
                tj3.Left = 16;
                if (this.Height < 280)
                {
                    this.Height += 40;
                    this.btnSelect.Top += 40;
                    this.btnClose.Top += 40;
                }
                ComboBox cmb3 = new ComboBox();
                cmb3.Name = "cmbTiaojian3";
                cmb3.Items.Add("联系电话");
                cmb3.Items.Add("性    别");
                cmb3.Items.Add("身份证号");
                cmb3.Items.Add("电子邮箱");
                cmb3.Left = 87;
                cmb3.Top = 196;
                cmb3.DropDownStyle = ComboBoxStyle.DropDownList;
                Label lb3 = new Label();
                lb3.Name = "lbMaohao3";
                lb3.Text = "：";
                lb3.Top = 199;
                lb3.Width = 20;
                lb3.Left = 210;
                TextBox tx3 = new TextBox();
                tx3.Name = "txtDafu3";
                tx3.Top = 196;
                tx3.Left = 237;
                tx3.Width = 120;
                Button bt3 = new Button();
                bt3.Click += Delete3;
                bt3.Top = 196;
                bt3.Left = 370;
                bt3.Text = "删除";
                bt3.Name = "btnDelete3";
                this.Controls.Add(cmb3);
                this.Controls.Add(lb3);
                this.Controls.Add(tx3);
                this.Controls.Add(tj3);
                this.Controls.Add(bt3);
                return;
            }
            label10.Visible = true;
        }


        private void Delete(object sender, EventArgs e)
        {
            foreach (Control item in Controls)
            {
                string name = item.Name;
                if (name == "lbTj1")
                {
                    item.Visible = false;
                }
                if (name == "cmbTiaojian1")
                {
                    item.Visible = false;
                }
                if (name == "lbMaohao1")
                {
                    item.Visible = false;
                }
                if (name == "txtDafu1")
                {
                    item.Visible = false;
                }
                if (name == "btnDelete1")
                {
                    item.Visible = false;
                }

            }
            this.label10.Visible = false;
            string s = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj1" && item.Visible == true || item.Name == "lbTj2" && item.Visible == true || item.Name == "lbTj3" && item.Visible == true)
                {
                    s += item.Name;
                }
            }
            if (s == "")
            {
                this.Height = 184;
                this.btnSelect.Top = 112;
                this.btnClose.Top = 112;
            }

        }
        private void Delete2(object sender, EventArgs e)
        {
            foreach (Control item in Controls)
            {
                string name = item.Name;
                if (name == "lbTj2")
                {
                    item.Visible = false;
                }
                if (name == "cmbTiaojian2")
                {
                    item.Visible = false;
                }
                if (name == "lbMaohao2")
                {
                    item.Visible = false;
                }
                if (name == "txtDafu2")
                {
                    item.Visible = false;
                }
                if (name == "btnDelete2")
                {
                    item.Visible = false;
                }

            }
            this.label10.Visible = false;
            string s = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj1" && item.Visible == true || item.Name == "lbTj2" && item.Visible == true || item.Name == "lbTj3" && item.Visible == true)
                {
                    s += item.Name;
                }
            }
            if (s == "")
            {
                this.Height = 184;
                this.btnSelect.Top = 112;
                this.btnClose.Top = 112;
            }
        }
        private void Delete3(object sender, EventArgs e)
        {
            foreach (Control item in Controls)
            {
                string name = item.Name;
                if (name == "lbTj3")
                {
                    item.Visible = false;
                }
                if (name == "cmbTiaojian3")
                {
                    item.Visible = false;
                }
                if (name == "lbMaohao3")
                {
                    item.Visible = false;
                }
                if (name == "txtDafu3")
                {
                    item.Visible = false;
                }
                if (name == "btnDelete3")
                {
                    item.Visible = false;
                }

            }
            this.label10.Visible = false;
            string s = "";
            foreach (Control item in Controls)
            {
                if (item.Name == "lbTj1" && item.Visible == true || item.Name == "lbTj2" && item.Visible == true || item.Name == "lbTj3" && item.Visible == true)
                {
                    s += item.Name;
                }
            }
            if (s == "")
            {
                this.Height = 184;
                this.btnSelect.Top = 112;
                this.btnClose.Top = 112;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void txttel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }
    }
}
